// pages/Index/shopActive/invitation/invitation_send/invitation_send.js
const controller = require('../../../../controllers/controllerIndex').controller

Page({

  data: {
    invitationId: '',
    giveNum: 0,
    totalNum: 0,
    sendList: [],
    num: '',
  },

  onLoad: function (options) {
    console.log(options);
    this.setData({
      invitationId: options.invitationId,
      giveNum: options.giveNum,
      totalNum: options.totalNum,
    })
  },

  onShow: function (options) {
    this.getSendList()
  },
  // 获取发送人列表
  getSendList() {
    let sendList = this.data.sendList
    let selectList = wx.getStorageSync('selectList')
    if (!selectList) return
    wx.removeStorageSync('selectList')
    selectList = JSON.parse(selectList)

    // 过滤重复项
    selectList = selectList.filter(item1 => {
      return !sendList.some(item2 => item1.id == item2.id)
    })

    sendList = sendList.concat(selectList)
    this.setData({
      sendList
    })
  },
  getSendNum(e) {
    this.setData({
      num: e.detail.value
    })
  },
  // 删除项
  delete(e) {
    const index = e.currentTarget.dataset.index
    let sendList = this.data.sendList
    sendList.splice(index, 1)
    this.setData({
      sendList
    })
  },

  addPerson() {
    wx.navigateTo({
      url: './invitation_addPerson/invitation_addPerson',
    })
  },
  send() {
    const invitationId=Number(this.data.invitationId);
    const num=Number(this.data.num);
    const sendList=this.data.sendList;
    if(num <= 0||!sendList.length){
      wx.showToast({
        title: '请完善信息',
      })
      return
    }
    let accountIds = []
    sendList.forEach(item=>{
      accountIds.push(item.accountId)
    })
    accountIds=accountIds.join(',')
    let value = {
      shopId: wx.getStorageSync('shopId'),
      sessionId: wx.getStorageSync('token'),
      invitationId,
      num,
      accountIds,
    }
    controller.shopSendInvitation(value).then(res => {
      wx.showToast({
        title: '发送成功',
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1000);
    })
  }

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})